﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZkSurvey.Common.SurveyMgr;
using ZkSurvey.DAL;
using ZkSurvey.DAL.SurveyMgr;

namespace ZkSurvey.BLL.SurveyMgr
{
    public class AnswerBl
    {
        public void CreateAnswer(Answer answer)
        {

        }

        public void CreateAnswers(Participation partic, List<Answer> answers)
        {
            partic.JoinTime = DateTime.Now;
            using (TranscationHelper trans = TranscationHelper.GetInstance())
            {
                trans.BeginTrans();
                try
                {
                    ParticipationDa.InsertParticipation(partic, trans);

                    foreach (Answer answer in answers)
                    {
                        answer.ParticipationId = partic.Id;
                        AnswerDa.InsertAnswer(answer, trans);
                    }

                    trans.CommitTrans();
                }
                catch
                {
                    trans.RollTrans();
                    throw;
                }
            }
        }

        public Answer GetAnswer(int particId, int quesId)
        {
            List<Answer> answers = AnswerDa.GetAnswer(particId, quesId);
            return answers.Count > 0 ? answers[0] : null;
        }

        public List<Answer> GetAnswerList(int particId)
        {
            return AnswerDa.GetAnswer(particId, null);
        }

        public List<Answer> GetValidQuestionAnswerList(int quesId)
        {
            List<Answer> answers = AnswerDa.GetAnswer(null, quesId).FindAll(a => ParticipationDa.GetParticipation(a.ParticipationId).IsValid.GetValueOrDefault());
            return answers;
        }
    }
}
